package projectodedalo.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import projectodedalo.modelo.Idea;
import projectodedalo.negocio.GestionEstado;
import projectodedalo.negocio.GestionIdea;

/**
 * Servlet implementation class BuscarIdeasServlet
 */
@WebServlet("/BuscarIdeasServlet")
public class BuscarIdeasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuscarIdeasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String xPorEstado = request.getParameter("porEstado");
		String txtPalabras = request.getParameter("txtPalabrasBusqueda");

		
		if (xPorEstado==null) {
			xPorEstado="0";
		}
		if (txtPalabras==null) {
			txtPalabras="";
		}
		txtPalabras.toUpperCase();

		System.out.println("BuscarIdeaServlet doPost: " + xPorEstado + " -" + txtPalabras+"-");
		
		try {
			GestionIdea ideas = new GestionIdea();		
			ArrayList<Idea> resultado = ideas.listarTodosBuscar(xPorEstado,txtPalabras);	
			System.out.println("BuscarIdeaServlet size:"+resultado.size());
			System.out.println("BuscarIdeaServlet: Almacena datos para el JSP");
			request.setAttribute("LISTA", resultado);
			RequestDispatcher rd = request.getRequestDispatcher("buscarIdeas.jsp");
			rd.forward(request, response);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

		
		

}
